package com.manli.api.service;

import com.manli.api.pojo.SessionInfo;
import com.manli.api.util.SessionInfoUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.BuyCarLogDao;
import com.manli.api.dto.BuyCarLog;

import javax.servlet.http.HttpServletRequest;


@Service("buyCarLogService")
public class BuyCarLogService {
    @Autowired
    BuyCarLogDao buyCarLogDao;
    @Autowired
    SQLManager sqlManager;
    @Autowired
    SessionInfoUtils sessionInfoUtils;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from buy_car_log where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<BuyCarLog> pageQuery = new PageQuery<BuyCarLog>(params.getPage(),params.getLimit());
        PageQuery<BuyCarLog> query =sqlManager.execute(sqlReady,BuyCarLog.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<BuyCarLog> list = buyCarLogDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer buyCarLogId) {
        BuyCarLog buyCarLog = buyCarLogDao.single(buyCarLogId);
        return ResultUtil.success(buyCarLog);
    }

    @Transactional
    public Result save(BuyCarLog buyCarLog, HttpServletRequest request) {
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        BuyCarLog sourceBuyCarLog = buyCarLogDao.createLambdaQuery()
                .andEq("unionid",sessionInfo.getWeChatUserInfo().getUnionid())
                .andEq("goods_def_id" , buyCarLog.getGoodsDefId())
                .andEq("goods_model_id" , buyCarLog.getGoodsModelId())
                .andEq("status",1).single();
        if(sourceBuyCarLog == null){
            buyCarLog.setOpenid(sessionInfo.getWeChatUserInfo().getOpenid());
            buyCarLog.setUnionid(sessionInfo.getWeChatUserInfo().getUnionid());
            buyCarLog.setStatus(1);
            buyCarLog.setCreateTime(new Date());
            buyCarLogDao.insert(buyCarLog);
        }else{
            sourceBuyCarLog.setBuyCount(sourceBuyCarLog.getBuyCount()+buyCarLog.getBuyCount());
            sourceBuyCarLog.setStatus(1);
            sourceBuyCarLog.setCreateTime(new Date());
            buyCarLogDao.updateById(sourceBuyCarLog);
        }
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(BuyCarLog buyCarLog) {
        buyCarLogDao.updateTemplateById(buyCarLog);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(BuyCarLog buyCarLog) {
        buyCarLogDao.updateTemplateById(buyCarLog);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            buyCarLogDao.deleteById(id);
        }
        return ResultUtil.success();
    }



}
